#include <stdio.h>
#include "monster.h"
#include <time.h>
#include <stdbool.h>

/*创建怪物类型*/
monster* CreatMonster(const char* name,int HP,int attack,int def,const char* symbol,int exp,int gold)
{
    monster* m = (monster*)malloc(sizeof(monster));
    if(m==NULL)
    {
        return NULL;
    }
    m->HP = HP;                      //生命值
    // m->HPMax = HPMax;
    m->attack = attack;              //攻击力
    m->symbol = GetString(symbol);             //图标
    m->name = GetString(name);      //怪物名
    m->exp = exp;                   //持有经验
    m->gold = gold;                 //持有金币
    m->def = def;                   //防御力
    return m; 
}

//从存档重塑怪物
monster* ReCreatMonster(int HP,int Attack,int X,int Y,const char* Symbol,const char* Name,int Exp,int Gold,int Def)
{
    monster* m = (monster*)malloc(sizeof(monster));
    if(m==NULL)
    {
        return NULL;
    }
    m->HP = HP;                      //生命值
    m->attack = Attack;              //攻击力
    m->x = X;
    m->y = Y;
    m->symbol = GetString(Symbol);             //图标
    m->name = GetString(Name);      //怪物名
    m->exp = Exp;                   //持有经验
    m->gold = Gold;                 //持有金币
    m->def = Def;                   //防御力
    return m; 
}


void FreeMonster(void* ptr)
{
    monster* m = (monster*)ptr;
    ClearString(&m->name);
    free(m);
}

void SetRandPosition(void* ptr)
{
    monster* m = (monster*)ptr;
        m->x = rand()%10;
        m->y = rand()%10;
    while(m->x == 0 && m->y == 0)
    {
        m->x = rand()%10;
        m->y = rand()%10;
    }
}

bool SortByPoint(void* ptr1,void* ptr2)
{
    monster* m1 = (monster*)ptr1;
    monster* m2 = (monster*)ptr2;
    if(m1->x >m2->x)
    {
        return true;
    }
    else if(m1->x==m2->x)
    {
        return m1->y > m2->y;
    }
    else
    {
        return false;
    }
}

void PrintMonster(void* ptr)
{
    monster* ms = (monster*)ptr;
    printf("name:%s x: %d y: %d symbol:%s\n",ms->name.string,ms->x+1,ms->y+1,ms->symbol.string);
}

/*随机设置怪物(新游戏)*/
void SetMonsters(DArray* ms)
{
    /*随机数种子*/
    srand(time(NULL));

    for(int i=0;i<10;i++)
    {
        ms->InsertArrayTail(ms,CreatMonster("下级士兵游魂",20,5,0,"  🧟 ",10,4));
    }
    for(int i=0;i<6;i++)
    {
        ms->InsertArrayTail(ms,CreatMonster("洛斯里克骑士",60,10,2,"  👾 ",20,9));
    }
    for(int i=0;i<4;i++)
    {
        ms->InsertArrayTail(ms,CreatMonster("黑骑士",120,15,8,"  👿 ",40,16));
    }

    ms->InsertArrayTail(ms,CreatMonster("英雄古达",200,20,10,"  🕺 ",0,0));

    ms->DisplayDynamicArray(ms,SetRandPosition);
}


static char* (*MyMap)[MAPSIZE] = NULL;
void SetPosition(void* ptr)
{
    monster* m = (monster*)ptr;
    if(m->HP>0)
    {
        
        MyMap[m->x][m->y] = m->symbol.string;
    }
}


/*重新在棋盘上设置怪物*/
void ResetMonsterSPosition(DArray* ms,char* (*map)[MAPSIZE])
{
    MyMap = map;
 
    ms->DisplayDynamicArray(ms,SetPosition);
    MyMap = NULL;

    printf("怪物已装载!\n");
    sleep(1);
}

/*处理重复位置的怪物(去重复)*/
void SetPositionDumplicate(DArray* ms)
{
    int flag = 0;
    ms->Sort(ms,SortByPoint);
    for(int i=0;i<ms->len-1;i++)
    {
        monster* m1 = (monster*)ms->dp[i];
        monster* m2 = (monster*)ms->dp[i+1];

        if(m1->x == m2->x && m1->y == m2->y)
        {
            m2->y++;
            if(m2->y == MAPSIZE)
            {
                flag=1;
                m2->x++;
                m2->x = m2->x%MAPSIZE;
                m2->y = 1;
            }
        }
    }
    if(flag)
    {
        SetPositionDumplicate(ms);
    }
}

/*在棋盘上设置怪物*/
void SetMonstersPosition(DArray* ms,char* (*map)[MAPSIZE])
{
    /*先处理位置重叠的怪*/
    SetPositionDumplicate(ms);

    MyMap = map;
    ms->DisplayDynamicArray(ms,SetPosition);
    MyMap = NULL;

    printf("怪物已装载!\n");
    /*打印怪物列表*/
    // ms->DisplayDynamicArray(ms,PrintMonster);
}

/*根据人物位置获取怪物信息*/
monster* GetMonsterByPosition(DArray *ms,int x,int y)
{
    for(int i=0;i<ms->len;i++)
    {
        monster* m1 = (monster*)ms->dp[i];
        if(m1->x == x && m1->y == y && m1->HP>0)
        {
            return m1;
        }
    }
    return NULL;
}

/*把怪物状态保存至字符串数组*/
mystr MonsterToString(DArray* ms)
{
    char str[256] = {0};
    char list[4096] = {0};
    for(int i=0;i<ms->len;i++)
    {
        monster* m = (monster*)ms->dp[i];
        sprintf(str,"%d %d %d %d %s %d %d %d %s\n",m->HP,m->attack,m->x,m->y,m->name.string,m->exp,m->gold,m->def,m->symbol.string);
        
        strcat(list,str);
    }
    // /*显示怪物存档信息*/
    // printf("%s\n",list);
    return GetString(list);
}

/*从文件解析怪物字符串数组*/
void StringToMonster(mystrlist* mst,DArray* ms)
{
    //解析怪物数组之前首先对需要存入的数组初始化
    InitDynamicArray(ms);
    int TempHp = 0;
    int TempAttack = 0;
    int TempX = 0;
    int TempY = 0;
    
    char TempName[32] = {0};
    int TempExp = 0;
    int TempGold = 0;
    int TempDef = 0;
    
    for(int i=0;i<mst->len;i++)
    {   
        char* TempSymbol;
        char symbol[32];
        TempSymbol = symbol;
        char sym[32] ="  ";

        sscanf(mst->stringlist[i].string,"%d %d %d %d %s %d %d %d %s",&TempHp,&TempAttack,&TempX,&TempY,TempName,&TempExp,&TempGold,&TempDef,TempSymbol);
        
        strcat(TempSymbol," ");
        strcat(sym,TempSymbol);

        ms->InsertArrayTail(ms,ReCreatMonster(TempHp,TempAttack,TempX,TempY,sym,TempName,TempExp,TempGold,TempDef));
    }
}

